### REPLICATION FILE -- MAIN ANALYSIS -- STUDY 1, EXPERIMENT
### Homola, Rogowski, Sinclair, Torres, Tucker, and Webster
### "Through the Ideology of the Beholder: How Ideology Shapes Perceptions of Partisan Groups"
### Political Science Research and Methods

## clear environment, set seed, install/load packages
rm(list=ls())
library(plyr); library(weights); library(stargazer); library(interplot)

# log file/sink
sink("HRSTTW_Stereotypes_Study1_R.txt")

# Load data
fdata <- read.csv("HRSTTW_Stereotypes_Study1.csv")

# Set up analysis for Figure 1
vars <- c("friendsbin", "employerbin", "childrenbin", "datingbin", "dinnerbin")
attach(fdata)
friends.pid <-
  wtd.t.test(friendsbin[xstereo == 2],
             friendsbin[xstereo == 1],
             dec2016wt1[xstereo == 2],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
friends.stereo <-
  wtd.t.test(friendsbin[xstereo == 3],
             friendsbin[xstereo == 1],
             dec2016wt1[xstereo == 3],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
employer.pid <-
  wtd.t.test(employerbin[xstereo == 2],
             employerbin[xstereo == 1],
             dec2016wt1[xstereo == 2],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
employer.stereo <-
  wtd.t.test(employerbin[xstereo == 3],
             employerbin[xstereo == 1],
             dec2016wt1[xstereo == 3],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
children.pid <-
  wtd.t.test(childrenbin[xstereo == 2],
             childrenbin[xstereo == 1],
             dec2016wt1[xstereo == 2],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
children.stereo <-
  wtd.t.test(childrenbin[xstereo == 3],
             childrenbin[xstereo == 1],
             dec2016wt1[xstereo == 3],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
dating.pid <-
  wtd.t.test(datingbin[xstereo == 2],
             datingbin[xstereo == 1],
             dec2016wt1[xstereo == 2],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
dating.stereo <-
  wtd.t.test(datingbin[xstereo == 3],
             datingbin[xstereo == 1],
             dec2016wt1[xstereo == 3],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
dinner.pid <-
  wtd.t.test(dinnerbin[xstereo == 2],
             dinnerbin[xstereo == 1],
             dec2016wt1[xstereo == 2],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
dinner.stereo <-
  wtd.t.test(dinnerbin[xstereo == 3],
             dinnerbin[xstereo == 1],
             dec2016wt1[xstereo == 3],
             dec2016wt1[xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]

effects.pid <- c(friends.pid[1],employer.pid[1],children.pid[1],dating.pid[1],dinner.pid[1])
effects.stereo <- c(friends.stereo[1],employer.stereo[1],children.stereo[1],dating.stereo[1],
                    dinner.stereo[1])
se.pid <- c(friends.pid[2],employer.pid[2],children.pid[2],dating.pid[2],dinner.pid[2])
se.stereo <- c(friends.stereo[2],employer.stereo[2],children.stereo[2],dating.stereo[2],
               dinner.stereo[2])

catNames <- c("Friends", "Hire", "Children", "Dating", "Dinner")


###
### Figure 1: Effect of Partisanship and Ideology on Interpersonal Evaluations
###

pdf("Figure1_TreatEffects_TAPS_overall_320.pdf", height = 6, width = 8)

layout(matrix(c(1,2), ncol=1, byrow = TRUE), heights=c(0.9,0.1))
par(oma=c(1,2,0.3,1), mar=c(3,2,0.3,1))
plot(0,0, type="n",
     xaxt="n", yaxt="n", xlab="", ylab="", main="",
     xlim=c(-0.35, 0.15),
     ylim=c(0,5))

# Shading
rect(xleft=-0.55, xright=0.25, ybottom=1, ytop=2, col="gray90")
rect(xleft=-0.55, xright=0.25, ybottom=3, ytop=4, col="gray90")

# Evil 0
abline(v=0, col="GREY", lwd=2,lty=2)

# Treatment 2: PID
points(effects.pid, seq(0.6,4.6,1), pch=17,cex=1.5)
segments(effects.pid-1.96*se.pid, seq(0.6,4.6,1), effects.pid+1.96*se.pid, seq(0.6,4.6,1))

# Treatment 3: PID + ideology
points(effects.stereo, seq(0.4,4.4,1), pch=16,cex=1.5)
segments(effects.stereo-1.96*se.stereo, seq(0.4,4.4,1), effects.stereo+1.96*se.stereo, seq(0.4,4.4,1))

# Axis
axis(1, tck=0, cex.axis=1, cex.lab=1, mgp=c(0.3, 0.3, 0), lty=0)
axis(2, tck=0, cex.axis=1, cex.lab=1, 
     at = seq(0.5,4.5,1), labels=catNames,
     mgp=c(0.3, 0.3, 0), lty=1, las=1)
axis(4, tck=0, cex.axis=1, lty=1, labels=NA)
title(xlab = "Treatment effect", line = 1.5, cex.lab = 1, ylab="")

# Text
text(dinner.pid[1], 4.8, format(round(dinner.pid[1], digits=2),nsmall=2))
text(dinner.stereo[1], 4.2, format(round(dinner.stereo[1], digits=2),nsmall=2))
text(dating.pid[1], 3.8, format(round(dating.pid[1], digits=2),nsmall=2))
text(dating.stereo[1], 3.2, format(round(dating.stereo[1], digits=2),nsmall=2))
text(children.pid[1], 2.8, format(round(children.pid[1], digits=2),nsmall=2))
text(children.stereo[1], 2.2, format(round(children.stereo[1], digits=2),nsmall=2))
text(employer.pid[1], 1.8, format(round(employer.pid[1], digits=2),nsmall=2))
text(employer.stereo[1], 1.2, format(round(employer.stereo[1], digits=2),nsmall=2))
text(friends.pid[1], .8, format(round(friends.pid[1], digits=2),nsmall=2))
text(friends.stereo[1], .2, format(round(friends.stereo[1], digits=2),nsmall=2))

# Legend
par(mar=c(0,2,0,0))
plot(0,0, type="n", axes = FALSE, xlab = "", ylab = "")
legend("center", ncol = 2, 
       legend = c("PID condition",  "PID + ideology condition"),
       cex=1, pch=c(17,16), lty=1, bty="n") 

dev.off()


# Set up analysis for Figure 2
ddata <- fdata[fdata$xpidpan==2,]
rdata <- fdata[fdata$xpidpan==3,]
idata <- fdata[fdata$xpidpan==1,]
d.ideo.pid <-
  wtd.t.test(ddata$ideolneigh[ddata$xstereo == 2],
             ddata$ideolneigh[ddata$xstereo == 1],
             ddata$dec2016wt1[ddata$xstereo == 2],
             ddata$dec2016wt1[ddata$xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
d.ideo.stereo <-
  wtd.t.test(ddata$ideolneigh[ddata$xstereo == 3],
             ddata$ideolneigh[ddata$xstereo == 1],
             ddata$dec2016wt1[ddata$xstereo == 3],
             ddata$dec2016wt1[ddata$xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
r.ideo.pid <-
  wtd.t.test(rdata$ideolneigh[rdata$xstereo == 2],
             rdata$ideolneigh[rdata$xstereo == 1],
             rdata$dec2016wt1[rdata$xstereo == 2],
             rdata$dec2016wt1[rdata$xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]
r.ideo.stereo <-
  wtd.t.test(rdata$ideolneigh[rdata$xstereo == 3],
             rdata$ideolneigh[rdata$xstereo == 1],
             rdata$dec2016wt1[rdata$xstereo == 3],
             rdata$dec2016wt1[rdata$xstereo == 1],
             samedata = F)$additional[c("Difference", "Std. Err")]


###
### Figure 2: Effect of Partisanship and Ideology on Evaluations of Ideology
###

effects <- cbind(c(d.ideo.pid[1],d.ideo.stereo[1],r.ideo.pid[1],r.ideo.stereo[1]),
                 c(d.ideo.pid[2],d.ideo.stereo[2],r.ideo.pid[2],r.ideo.stereo[2]))

pdf("Figure2_TreatEffectsIdeol_TAPS_320_noI.pdf", height = 6, width = 8)
layout(matrix(1:2, ncol=1), heights=c(0.9,0.1))
par(oma=c(1,3,1,1), mar=c(4,3,1,1))
plot(0,0, type="n",
     xaxt="n", yaxt="n", xlab="", ylab="", main="",
     xlim=c(-1.6, 1.1),
     ylim=c(0,2))

rect(xleft=-2, xright=2, ybottom=1, ytop=2.5, col="gray90")

points(effects[1:4,1],c(1.6,0.6,1.6,0.6),pch=c(17,17,16,16),
       col="BLACK")

abline(v=0, col="GREY", lwd=2,lty=2)

segments(effects[1:4,1]-1.96*effects[1:4,2], c(1.6,0.6,1.6,0.6), 
         effects[1:4,1]+1.96*effects[1:4,2], c(1.6,0.6,1.6,0.6),
         col=c("BLACK","BLACK","BLACK","BLACK"))

axis(1, tck=0, cex.axis=0.7, cex.lab=0.7, mgp=c(0.3, 0.3, 0), lty=0)
axis(2, tck=0, cex.axis=0.9, cex.lab=0.7, 
     at = c(0.5, 1.5), labels=c('PID + ideology \n condition      ', 'PID         \n condition      '),
     mgp=c(0.3, 0.3, 0), lty=1, las=1)
title(xlab = "Treatment effects on ideological placement \n (negative=more liberal; positive=more conservative)", 
      line = 2.5, cex.lab = 0.8, ylab="")
box()

# Text
text(effects[1,1], 1.675, format(round(effects[1,1], digits=2),nsmall=2))
text(effects[2,1], 0.675, format(round(effects[2,1], digits=2),nsmall=2))
text(effects[3,1], 1.675, format(round(effects[3,1], digits=2),nsmall=2))
text(effects[4,1], 0.675, format(round(effects[4,1], digits=2),nsmall=2))

par(mar=c(0,2,0,0))
plot(0,0, type="n", axes = FALSE, xlab = "", ylab = "")
legend("center", ncol = 1, 
       legend = c("Democratic placements (Rep respondents)",  
                  "Republican placements (Dem respondents)"),
       cex=.85, pch=c(16,17), col="BLACK", bty="n") 

dev.off()


###
### Table A.1: Descriptive Statistics (TAPS Experiment)
###
stargazer(fdata[,c("FEMALE", "WHITE", "NEW_AGE", "YRSEDUC",
                   "INCOME", "IDEO7_PROF", "Democrat", "Republican")], omit.summary.stat=c('p25', 'p75'), digits=2,
          title="Table A.1")

###
### Table A.4: Summary Statistics Across Experimental Treatments (TAPS)
###
print("Table A.4")
round(c(mean(fdata$friendsbin[fdata$xstereo==1], na.rm=T), mean(fdata$employerbin[fdata$xstereo==1], na.rm=T), mean(fdata$childrenbin[fdata$xstereo==1], na.rm=T),
        mean(fdata$datingbin[fdata$xstereo==1], na.rm=T), mean(fdata$dinnerbin[fdata$xstereo==1], na.rm=T), mean(fdata$ideolneigh[fdata$xstereo==1], na.rm=T)), 2)
round(c(mean(fdata$friendsbin[fdata$xstereo==2], na.rm=T), mean(fdata$employerbin[fdata$xstereo==2], na.rm=T), mean(fdata$childrenbin[fdata$xstereo==2], na.rm=T),
        mean(fdata$datingbin[fdata$xstereo==2], na.rm=T), mean(fdata$dinnerbin[fdata$xstereo==2], na.rm=T), mean(fdata$ideolneigh[fdata$xstereo==2], na.rm=T)), 2)
round(c(mean(fdata$friendsbin[fdata$xstereo==3], na.rm=T), mean(fdata$employerbin[fdata$xstereo==3], na.rm=T), mean(fdata$childrenbin[fdata$xstereo==3], na.rm=T),
        mean(fdata$datingbin[fdata$xstereo==3], na.rm=T), mean(fdata$dinnerbin[fdata$xstereo==3], na.rm=T), mean(fdata$ideolneigh[fdata$xstereo==3], na.rm=T)), 2)


###
### Table A.5: Partisanship and the Heterogeneous Effect of Partisanship and Ideology on Social Interaction
###
fdata_regs <- subset(fdata, (Democrat==1 | Republican==1))
mod1 <- lm(friendsbin ~ as.factor(xstereo)*as.factor(xdr) + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
mod2 <- lm(employerbin ~ as.factor(xstereo)*as.factor(xdr) + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
mod3 <- lm(childrenbin ~ as.factor(xstereo)*as.factor(xdr) + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
mod4 <- lm(datingbin ~ as.factor(xstereo)*as.factor(xdr) + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
mod5 <- lm(dinnerbin ~ as.factor(xstereo)*as.factor(xdr) + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
stargazer(mod1, mod2, mod3, mod4, mod5,
          digits=2, star.cutoffs = c(0.05, NA, NA), dep.var.labels.include = FALSE, no.space=T,
          omit.stat=c("adj.rsq", "f", "ser"), title="Table A.5")


###
### Table A.6: Strength of Partisanship and the Heterogeneous Effect of Partisanship and Ideology on Social Interaction
###
fdata$strong<-NA
fdata$strong[fdata$PARTYID2S61=="Not Very Strong"]<-0
fdata$strong[fdata$PARTYID2S61=="Strong"]<-1
fdata_regs <- subset(fdata, (Democrat==1 | Republican==1))
mod1 <- lm(friendsbin ~ as.factor(xstereo)*strong + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
mod2 <- lm(employerbin ~ as.factor(xstereo)*strong + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
mod3 <- lm(childrenbin ~ as.factor(xstereo)*strong + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
mod4 <- lm(datingbin ~ as.factor(xstereo)*strong + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
mod5 <- lm(dinnerbin ~ as.factor(xstereo)*strong + FEMALE + WHITE + NEW_AGE + YRSEDUC + INCOME, data=fdata_regs)
stargazer(mod1, mod2, mod3, mod4, mod5,
          digits=2, star.cutoffs = c(0.05, NA, NA), dep.var.labels.include = FALSE, no.space=T,
          omit.stat=c("adj.rsq", "f", "ser"), title="Table A.6")



###
### Figure A.1: Effect of Partisanship and Ideology on Interpersonal Evaluations by Ideological Extremity
###
extreme <- fdata[fdata$extreme==1,]
noextreme <- fdata[fdata$extreme==0,]

## EXTREME
mod0 <- llply(vars, function(x) lm(as.formula(paste(x, "~", 'TREAT3 + extreme + TREAT3:extreme')), data=fdata))
mod1 <- llply(vars, function(x) summary(lm(as.formula(paste(x, "~", 'TREAT3 + extreme + TREAT3:extreme')), data=fdata)))
inter_ls <- llply(mod0, function(x) interplot(x, 'TREAT3', 'extreme', ci=0.9))
inter_ls <- do.call(rbind, llply(inter_ls, function(x) x$data))
ttests_1x <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT3")), 
                                      data=extreme[extreme$TREAT3!=1,]))
diff_1x <- do.call(rbind,lapply(ttests_1x, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_1x <- cbind(diff_1x, diff_1x[,2]-diff_1x[,1])

ttests_2x <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT3")), 
                                      data=extreme[extreme$TREAT3!=2,]))
diff_2x <- do.call(rbind,lapply(ttests_2x, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_2x <- cbind(diff_2x, diff_2x[,2]-diff_2x[,1])

## NOT EXTREME
ttests_1nx <- llply(vars,
                    function(x) t.test(as.formula(paste(x, "~", "TREAT3")), 
                                       data=noextreme[noextreme$TREAT3!=1,]))
diff_1nx <- do.call(rbind,lapply(ttests_1nx, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_1nx <- cbind(diff_1nx, diff_1nx[,2]-diff_1nx[,1])

ttests_2nx <- llply(vars,
                    function(x) t.test(as.formula(paste(x, "~", "TREAT3")), 
                                       data=noextreme[noextreme$TREAT3!=2,]))
diff_2nx <- do.call(rbind,lapply(ttests_2nx, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_2nx <- cbind(diff_2nx, diff_2nx[,2]-diff_2nx[,1])

pdf("FigureA1_TreatEffects_extreme.pdf", height = 6, width = 6)
layout(matrix(c(1,2,3,3), ncol=2, byrow = TRUE), heights=c(0.9,0.1))
par(oma=c(1,2,0.3,1), mar=c(3,2,0.3,1))

# Extreme
plot(0,0, type="n",
     xaxt="n", yaxt="n", xlab="", ylab="", main="",
     xlim=c(-0.7, 0.2),
     ylim=c(0,5))
abline(h=1:4, col="gray60", lty=2)

# Treatment 2: PID+Stereotype
points(diff_1x[,5], seq(0.3,4.3,1), pch=16)
segments(diff_1x[,4], seq(0.3,4.3,1), diff_1x[,3], seq(0.3,4.3,1))

# Treatment 1 3: Only PID
points(diff_2x[,5], seq(0.7,4.7,1), pch=17)
segments(diff_2x[,4], seq(0.7,4.7,1), diff_2x[,3], seq(0.7,4.7,1), lty=2)

# Evil 0
abline(v=0, col="red")

# Axis
axis(1, tck=0, cex.axis=0.7, cex.lab=0.7, mgp=c(0.3, 0.3, 0), lty=0)
axis(2, tck=0, cex.axis=0.7, cex.lab=0.7, 
     at = seq(0.5,4.5,1), labels=catNames,
     mgp=c(0.3, 0.3, 0), lty=0)
title(xlab = "Treatment effect [Extreme]", line = 1.5, cex.lab = 0.8, ylab="")

# No extreme
plot(0,0, type="n",
     xaxt="n", yaxt="n", xlab="", ylab="", main="",
     xlim=c(-0.7, 0.2),
     ylim=c(0,5))
abline(h=1:4, col="gray60", lty=2)

# Treatment 2: PID+Stereotype
points(diff_1nx[,5], seq(0.3,4.3,1), pch=16)
segments(diff_1nx[,4], seq(0.3,4.3,1), diff_1nx[,3], seq(0.3,4.3,1))

# Treatment 1 3: Only PID
points(diff_2nx[,5], seq(0.7,4.7,1), pch=17)
segments(diff_2nx[,4], seq(0.7,4.7,1), diff_2nx[,3], seq(0.7,4.7,1), lty=2)

# Evil 0
abline(v=0, col="red")

# Axis
axis(1, tck=0, cex.axis=0.7, cex.lab=0.7, mgp=c(0.3, 0.3, 0), lty=0)
axis(2, tck=0, cex.axis=0.7, cex.lab=0.7, 
     at = seq(0.5,4.5,1), labels=catNames,
     mgp=c(0.3, 0.3, 0), lty=0)
title(xlab = "Treatment effect [Not extreme]", line = 1.5, cex.lab = 0.8, ylab="")

par(mar=c(0,2,0,0))
plot(0,0, type="n", axes = FALSE, xlab = "", ylab = "")
legend("center", ncol = 2, 
       legend = c("PID condition",  "PID + ideology condition"),
       cex=0.7, pch=c(17,16), lty=c(2,1), bty="n") 
dev.off()



###
### Figure A.2: Effect of Partisanship and Ideology on Interpersonal Evaluations by PID
###

## AMONG REPUBLICANS
ttests_1r <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT1")), 
                                      data=rdata[rdata$TREAT1!=7,]))
diff_1r <- do.call(rbind,lapply(ttests_1r, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_1r <- cbind(diff_1r, diff_1r[,2]-diff_1r[,1])

ttests_2r <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT1")), 
                                      data=rdata[rdata$TREAT1!=8,]))
diff_2r <- do.call(rbind,lapply(ttests_2r, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_2r <- cbind(diff_2r, diff_2r[,2]-diff_2r[,1])

ttests_3r <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT1")), 
                                      data=rdata[rdata$TREAT1!=0,]))
diff_3r <- do.call(rbind,lapply(ttests_3r, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_3r <- cbind(diff_3r, diff_3r[,2]-diff_3r[,1])


## AMONG DEMOCRATS
ttests_1d <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT1")), 
                                      data=ddata[ddata$TREAT1!=5,]))
diff_1d <- do.call(rbind,lapply(ttests_1d, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_1d <- cbind(diff_1d, diff_1d[,2]-diff_1d[,1])

ttests_2d <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT1")), 
                                      data=ddata[ddata$TREAT1!=6,]))
diff_2d <- do.call(rbind,lapply(ttests_2d, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_2d <- cbind(diff_2d, diff_2d[,2]-diff_2d[,1])

ttests_3d <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT1")), 
                                      data=ddata[ddata$TREAT1!=0,]))
diff_3d <- do.call(rbind,lapply(ttests_3d, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_3d <- cbind(diff_3d, diff_3d[,2]-diff_3d[,1])

## AMONG INDEPENDENTS (collapsed)
ttests_1i <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT2")), 
                                      data=idata[as.character(idata$TREAT2)!='1',]))
diff_1i <- do.call(rbind,lapply(ttests_1i, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_1i <- cbind(diff_1i, diff_1i[,2]-diff_1i[,1])

ttests_2i <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT2")), 
                                      data=idata[as.character(idata$TREAT2)!='3',]))
diff_2i <- do.call(rbind,lapply(ttests_2i, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_2i <- cbind(diff_2i, diff_2i[,2]-diff_2i[,1])

ttests_3i <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT2")), 
                                      data=idata[as.character(idata$TREAT2)!='0',]))
diff_3i <- do.call(rbind,lapply(ttests_3i, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_3i <- cbind(diff_3i, diff_3i[,2]-diff_3i[,1])


### Plot 1 [Democrats]
catNames <- c("Friends", "Hire", "Children", "Dating", "Dinner")
pdf("FigureA2_TreatEffects_TAPS_split.pdf")
layout(matrix(c(1,1,2,2,3,4,4,5,6,6,6,6), ncol=4, byrow = TRUE), heights=c(0.45,0.45,0.1))
par(oma=c(1,2,0.3,1), mar=c(3,2,0.3,1))
plot(0,0, type="n",
     xaxt="n", yaxt="n", xlab="", ylab="", main="",
     xlim=c(-0.7, 0.2),
     ylim=c(0,5))
abline(h=1:4, col="gray60", lty=2)

# Treatment 2: PID+Stereotype
points(diff_1d[,5], seq(0.3,4.3,1), pch=16)
segments(diff_1d[,4], seq(0.3,4.3,1), diff_1d[,3], seq(0.3,4.3,1))

# Treatment 1 3: Only PID
points(diff_2d[,5], seq(0.7,4.7,1), pch=17)
segments(diff_2d[,4], seq(0.7,4.7,1), diff_2d[,3], seq(0.7,4.7,1), lty=2)

# Evil 0
abline(v=0, col="red")

# Axis
axis(1, tck=0, cex.axis=0.7, cex.lab=0.7, mgp=c(0.3, 0.3, 0), lty=0)
axis(2, tck=0, cex.axis=0.7, cex.lab=0.7, 
     at = seq(0.5,4.5,1), labels=catNames,
     mgp=c(0.3, 0.3, 0), lty=0)
title(xlab = "Treatment effect (among Democrats)", line = 1.5, cex.lab = 0.8, ylab="")

#### Plot 2 [Republicans]
plot(0,0, type="n",
     xaxt="n", yaxt="n", xlab="", ylab="", main="",
     xlim=c(-0.7,0.2),
     ylim=c(0,5))
abline(h=1:4, col="gray60", lty=2)

# Treatment 2: PID+Stereotype
points(diff_1r[,5], seq(0.3,4.3,1), pch=16)
segments(diff_1r[,4], seq(0.3,4.3,1), diff_1r[,3], seq(0.3,4.3,1))

# Treatment 1 3: Only PID
points(diff_2r[,5], seq(0.7,4.7,1), pch=17)
segments(diff_2r[,4], seq(0.7,4.7,1), diff_2r[,3], seq(0.7,4.7,1), lty=2)

# Evil 0
abline(v=0, col="red")

# Axis
axis(1, tck=0, cex.axis=0.7, cex.lab=0.7, mgp=c(0.3, 0.3, 0), lty=0)
axis(2, tck=0, cex.axis=0.7, cex.lab=0.7, 
     at = seq(0.5,4.5,1), labels=catNames,
     mgp=c(0.3, 0.3, 0), lty=0)
title(xlab = "Treatment effect (among Republicans)", line = 1.5, cex.lab = 0.8, ylab="")

#### Place holder 1
plot(0,0, type = 'n', xlab='', ylab='', axes=FALSE)

#### Plot 3 [Independents]
plot(0,0, type="n",
     xaxt="n", yaxt="n", xlab="", ylab="", main="",
     xlim=c(-0.7,0.2),
     ylim=c(0,5))
abline(h=1:4, col="gray60", lty=2)

# Treatment 2: PID+Stereotype
points(diff_1i[,5], seq(0.3,4.3,1), pch=16)
segments(diff_1i[,4], seq(0.3,4.3,1), diff_1i[,3], seq(0.3,4.3,1))

# Treatment 1 3: Only PID
points(diff_2i[,5], seq(0.7,4.7,1), pch=17)
segments(diff_2i[,4], seq(0.7,4.7,1), diff_2i[,3], seq(0.7,4.7,1), lty=2)

# Evil 0
abline(v=0, col="red")

# Axis
axis(1, tck=0, cex.axis=0.7, cex.lab=0.7, mgp=c(0.3, 0.3, 0), lty=0)
axis(2, tck=0, cex.axis=0.7, cex.lab=0.7, 
     at = seq(0.5,4.5,1), labels=catNames,
     mgp=c(0.3, 0.3, 0), lty=0)
title(xlab = "Treatment effect (among Independents)", line = 1.5, cex.lab = 0.8, ylab="")

#### Place holder 1
plot(0,0, type = 'n', xlab='', ylab='', axes=FALSE)

par(mar=c(0,2,0,0))
plot(0,0, type="n", axes = FALSE, xlab = "", ylab = "")
legend("center", ncol = 2, 
       legend = c("PID condition",  "PID + ideology condition"),
       cex=0.7, pch=c(17,16), lty=c(2,1), bty="n") 
dev.off()



###
### Figure A.3: Effect of Partisanship and Ideology on Interpersonal Evaluations by Strength of Partisan Identity
###

strong <- fdata[fdata$strong==1,]
weak <- fdata[fdata$strong==0,]

## STRONG
mod0 <- llply(vars, function(x) lm(as.formula(paste(x, "~", 'TREAT3 + strong + TREAT3:strong')), data=fdata))
mod1 <- llply(vars, function(x) summary(lm(as.formula(paste(x, "~", 'TREAT3 + strong + TREAT3:strong')), data=fdata)))
inter_ls <- llply(mod0, function(x) interplot(x, 'TREAT3', 'strong', ci=0.9))
inter_ls <- do.call(rbind, llply(inter_ls, function(x) x$data))
ttests_1x <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT3")), 
                                      data=strong[strong$TREAT3!=1,]))
diff_1x <- do.call(rbind,lapply(ttests_1x, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_1x <- cbind(diff_1x, diff_1x[,2]-diff_1x[,1])

ttests_2x <- llply(vars,
                   function(x) t.test(as.formula(paste(x, "~", "TREAT3")), 
                                      data=strong[strong$TREAT3!=2,]))
diff_2x <- do.call(rbind,lapply(ttests_2x, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_2x <- cbind(diff_2x, diff_2x[,2]-diff_2x[,1])

## WEAK
ttests_1nx <- llply(vars,
                    function(x) t.test(as.formula(paste(x, "~", "TREAT3")), 
                                       data=weak[weak$TREAT3!=1,]))
diff_1nx <- do.call(rbind,lapply(ttests_1nx, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_1nx <- cbind(diff_1nx, diff_1nx[,2]-diff_1nx[,1])

ttests_2nx <- llply(vars,
                    function(x) t.test(as.formula(paste(x, "~", "TREAT3")), 
                                       data=weak[weak$TREAT3!=2,]))
diff_2nx <- do.call(rbind,lapply(ttests_2nx, function(x) c(x$estimate, (x$conf.int)*(-1))))
diff_2nx <- cbind(diff_2nx, diff_2nx[,2]-diff_2nx[,1])

pdf("FigureA3_strong_weak.pdf", height = 6, width = 6)
layout(matrix(c(1,2,3,3), ncol=2, byrow = TRUE), heights=c(0.9,0.1))
par(oma=c(1,2,0.3,1), mar=c(3,2,0.3,1))

# Strong
plot(0,0, type="n",
     xaxt="n", yaxt="n", xlab="", ylab="", main="",
     xlim=c(-0.7, 0.2),
     ylim=c(0,5))
abline(h=1:4, col="gray60", lty=2)

# Treatment 2: PID+Stereotype
points(diff_1x[,5], seq(0.3,4.3,1), pch=16)
segments(diff_1x[,4], seq(0.3,4.3,1), diff_1x[,3], seq(0.3,4.3,1))

# Treatment 1 3: Only PID
points(diff_2x[,5], seq(0.7,4.7,1), pch=17)
segments(diff_2x[,4], seq(0.7,4.7,1), diff_2x[,3], seq(0.7,4.7,1), lty=2)

# Evil 0
abline(v=0, col="red")

# Axis
axis(1, tck=0, cex.axis=0.7, cex.lab=0.7, mgp=c(0.3, 0.3, 0), lty=0)
axis(2, tck=0, cex.axis=0.7, cex.lab=0.7, 
     at = seq(0.5,4.5,1), labels=catNames,
     mgp=c(0.3, 0.3, 0), lty=0)
title(xlab = "Treatment effect [Strong]", line = 1.5, cex.lab = 0.8, ylab="")

# Weak
plot(0,0, type="n",
     xaxt="n", yaxt="n", xlab="", ylab="", main="",
     xlim=c(-0.7, 0.2),
     ylim=c(0,5))
abline(h=1:4, col="gray60", lty=2)

# Treatment 2: PID+Stereotype
points(diff_1nx[,5], seq(0.3,4.3,1), pch=16)
segments(diff_1nx[,4], seq(0.3,4.3,1), diff_1nx[,3], seq(0.3,4.3,1))

# Treatment 1 3: Only PID
points(diff_2nx[,5], seq(0.7,4.7,1), pch=17)
segments(diff_2nx[,4], seq(0.7,4.7,1), diff_2nx[,3], seq(0.7,4.7,1), lty=2)

# Evil 0
abline(v=0, col="red")

# Axis
axis(1, tck=0, cex.axis=0.7, cex.lab=0.7, mgp=c(0.3, 0.3, 0), lty=0)
axis(2, tck=0, cex.axis=0.7, cex.lab=0.7, 
     at = seq(0.5,4.5,1), labels=catNames,
     mgp=c(0.3, 0.3, 0), lty=0)
title(xlab = "Treatment effect [Weak]", line = 1.5, cex.lab = 0.8, ylab="")

par(mar=c(0,2,0,0))
plot(0,0, type="n", axes = FALSE, xlab = "", ylab = "")
legend("center", ncol = 2, 
       legend = c("PID condition",  "PID + ideology condition"),
       cex=0.7, pch=c(17,16), lty=c(2,1), bty="n") 
dev.off()

sink()